const App = {
    el: '#app',
    data: function () {
        return {
            menuData: [],
            menuDefaultProps: {
                children: 'childrens',
                label: 'name',
            },
            filterText: '',
            defaultExpandedKeys: [],
            defaultCheckedKeys: []
        };
    },
    methods: {
        onCheckedChange(currentNode,data){
            var testCaseId = $("#testCaseId").val();
            var param = {"unitTestIds":JSON.stringify(data.checkedKeys),"testCaseId":testCaseId};
            $.ajax({
                type: "post",
                async: true,
                contentType: "application/x-www-form-urlencoded; charset=utf-8",
                data:param,
                url:contextPath+" /test/testCase/unitTestCorrelate2testCase?ajaxTime="+new Date(),
                success: function(res){

                }
            });
        },
        getApiTree() {
            var testCaseId = $("#testCaseId").val();
            if(testCaseId!=""){
                var param = {"appName":appName,"testCaseId":testCaseId};
                var ajaxRes = ajaxRequest(contextPath+"/test/testCase/apiTree?ajaxTime="+new Date(),param);
                this.$refs.tree.setCheckedKeys([]);
                this.menuData = ajaxRes.menuTree.childrens;
                this.defaultExpandedKeys = ajaxRes.expandedKeys;
                this.defaultCheckedKeys = ajaxRes.checkedKeys;
            }else{
                this.menuData = [];
            }
        }
    },
    watch: {},
    mounted() {
    }
};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");

function getTestCases() {
    var env = $("#env").val();
    $.ajax({
        type: "get",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data:{},
        url:contextPath+"/test/testCases/"+appName+"/"+env+"?ajaxTime="+new Date(),
        success: function(testCases){
            var html = "";
            for(i=0;i<testCases.length;i++){
                var testCase = testCases[i];
                html += "<tr id=\"tr-"+testCase.id+"\" odd=\""+(i%2==0)+"\">";
                html += "<td style=\"text-align:left;\">";
                //  html += "<el-button style=\"width:100%\" @click=\"getApiTree('"+testCase.id+"')\">"+testCase.name+"</el-button>";
                html += "<a style=\"cursor:pointer;\" onclick=\"chooseTestCase('"+testCase.id+"','"+testCase.name+"')\">"+testCase.name+"</a>" ;
                html += "</td>";
                html += "<td width=\"20\">";
                html +="<a style=\"cursor:pointer;\" onclick=\"deleteTestCase('"+testCase.id+"')\">&#10005</a>";
                html += "</td>";
                html += "</tr>";
            }
            $("#testCases").html(html);
        }
    });
}


function saveTestCase() {
    var env = $("#env").val();
    var appName = appName
    var testCaseId = $("#testCaseId").val();
    var testCaseName = $("#testCaseName").val();
    if(testCaseName==""){
        alert("测试用例名不能为空");
        $("#testCaseName").focus();
        return;
    }
    var testCase = {"id":testCaseId,"name":testCaseName,"env":env,"appName":appName};
    $.ajax({
        type: "post",
        async: true,
        contentType:"application/json",
        data: JSON.stringify(testCase),
        url: contextPath+"/test/testCase?ajaxTime=" + new Date(),
        success: function (res) {
            if(res.status){
                alert("保存成功！");
                getTestCases();
                $("#btn_getApiTree").click();
            }else{
                alert("保存失败！");
            }
        }
    });
}

/**
 * 删除
 * */
function deleteTestCase(id) {
    if (!confirm("你确定要删除吗？")) {
        return;
    }
    $.ajax({
        type: "delete",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: {},
        url: contextPath+"/test/testCase/" + id + "?ajaxTime=" + new Date(),
        success: function (res) {
            if (res.status == true) {
                $("#tr-" + id).remove();
                cleanTestCase();
            }
        }
    });
}

function getCombinationTests() {
    $.ajax({
        type: "get",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: {},
        url: contextPath+"/test/combinationTests/"+appName+"/2?ajaxTime=" + new Date(),
        success: function (res) {
            var html = "";
            if (res.status == true) {
                for (i = 0; i < res.data.length; i++) {
                    var combinationTest = res.data[i];
                    var time = dateFormat(new Date(combinationTest.createDt));
                    html += "<tr id=\"tr-" + combinationTest.id + "\" odd=\"" + (i % 2 == 0) + "\">";
                    html += "<td style=\"text-align:left;\">";
                    html += "&#9672&nbsp;<a style=\"cursor:pointer;\" href='\/test\/report?testId=" + combinationTest.id + "' target=\"_blank\">" + combinationTest.name + "@" + time + "</a>";
                    html += "</td>";
                    html += "<td width=\"20\">";
                    html += "<a style=\"cursor:pointer;\" onclick=\"deleteCombinationTest('" + combinationTest.id + "')\">&#10005</a>";
                    html += "</td>";
                    html += "</tr>";
                }
            }
            $("#combinationTests").html(html);
        }
    });
}

function ajaxRequest(url,param) {
    var ajaxResult = null;
    $.ajax({
        type: "get",
        async: false,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data:param,
        url:url,
        success: function(res){
            ajaxResult = res;
        },error(e){
            ajaxResult = e;
        }
    });
    return ajaxResult;
}

function chooseTestCase(id,name) {
    $("#testCaseId").val(id);
    $("#testCaseName").val(name);
    $("#btn_getApiTree").click();
}
function cleanTestCase() {
    $("#testCaseId").val("");
    $("#testCaseName").val("");
    $("#btn_getApiTree").click();
}
/**
 * 删除
 * */
function deleteCombinationTest(id) {
    if (!confirm("你确定要删除吗？")) {
        return;
    }
    $.ajax({
        type: "delete",
        async: true,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data: {},
        url: contextPath+"/test/combinationTest/" + id + "?ajaxTime=" + new Date(),
        success: function (res) {
            if (res.status == true) {
                $("#tr-" + id).remove();
            }
        }
    });
}